﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using System.Configuration;
using Phoenix.ReflectionExtensions;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Logger;
using PServiceBus.Core.Configuration;
using PServiceBus.Core.Runtime.Configuration;

namespace PServiceBus.Core.Manager {
    public class MessageStoreManager {
        static List<IMessageStore> _stores;
        public static List<IMessageStore> Stores {
            get {
                if (_stores == null) {
                    _stores = new List<IMessageStore>();
                    foreach (var store in ConfigurationProvider.MessageStoreConfigs) {
                        if (!store.Enabled) continue;
                        var ex = MethodHelper.Try(() =>
                        {
                            var messageStore = DynamicBroker.Broker[store.Type].New() as IMessageStore;
                            messageStore.Address = store.Address;
                            messageStore.Init();
                            _stores.Add(messageStore);
                        });
                        ESBLogger.Log(ex);
                    }
                }
                return _stores;
            }
        }
    }
}
